/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import application.Facade;
import application.beans.CompteBean;
import application.beans.CoursBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Étienne
 */
public class ServSauvegarderCours extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        Facade f = new Facade();
        
        String next = ConstantesWeb.ATTRIBUT_PAGE_GESTIONCOURS;
        List<CoursBean> resultats = null;
        CoursBean criteres = new CoursBean();
        criteres.setProfesseur(new CompteBean());
        String elem = "";
        String nomProf = "";
        
        // On passe à travers les comptes affichés et on enregistre les modifications
        for(Enumeration e = request.getParameterNames(); e.hasMoreElements();){
            elem = (String)e.nextElement();
            if(elem.indexOf("identifiant") > -1){
                // On va chercher l'id du compte
                int identifiant = Integer.parseInt(request.getParameter(elem));
                criteres.setIdCours(identifiant);
                nomProf = request.getParameter("choixProf" + identifiant);
                criteres.getProfesseur().setIdCompte(f.chercherCompteUsername(nomProf).getIdCompte());
                if(request.getParameter("titre" + identifiant) != null){
                    criteres.setTitre(f.decoderChaine(request.getParameter("titre" + identifiant)));
                } else{
                    criteres.setTitre("");
                }
                if(request.getParameter("prof" + identifiant) != null){
                    criteres.getProfesseur().setNomComplet(f.decoderChaine(request.getParameter("prof" + identifiant)));
                } else{
                    criteres.getProfesseur().setNomComplet("");
                }
                if(request.getParameter("domaine" + identifiant) != null){
                    criteres.setDomaine(f.decoderChaine(request.getParameter("domaine" + identifiant)));
                } else{
                    criteres.setDomaine("");
                }
                if(request.getParameter("groupe" + identifiant) != null){
                    criteres.setGroupe(Integer.parseInt(request.getParameter("groupe" + identifiant)));
                } else{
                    criteres.setGroupe(1);
                }
                criteres.setCodeCours(request.getParameter("code" + identifiant));
                criteres.setActif(request.getParameter("actif" + identifiant));
                
                f.miseAJourCours(criteres);
            }
        }

        // On va chercher les résultats mis à jour de la recherche
        resultats = f.rechercheCoursAvancee((CoursBean)session.getAttribute(ConstantesWeb.ATTRIBUT_CRITERES_GESTION_COURS));
        session.setAttribute(ConstantesWeb.ATTRIBUT_RESULTAT_GESTION_COURS, resultats);
        
        RequestDispatcher rd = getServletContext().getRequestDispatcher(next);
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
